// Admin Controller

function categoryEditCtrl($http, $scope, $route, $rootScope, $timeout, commonService, $location) {
    
    $scope.fieldData = {};
    
    var param = $location.search();
    var url = 'category_ws.php';
    var catid  = '';
    
    if(param['catid']){
	catid = param['catid'];
            var params = {
               wsname : 'getcategorybyid',
               id : param['catid'],
               token : _pageData.tokenKey
            };
            $.getJSON(_COMMON.context + url, params).done(function(result) {
        	$scope.$apply(function() {
        	    $scope.fieldData = result.categories;
        	});
            });
            
            $("#name").prop("readonly",true);
    }
    

   $scope.save = function() {
	var data = $scope.fieldData;
	
	var config = {
	    params : {
		wsname : catid==''? 'createcategory':'updatecategory',
		token : _pageData.tokenKey,
		id : catid,
		name : data.name ? data.name : '',
		title : data.name ? data.title : '',
		description : data.description ? data.description : ''
	    }
	};
	
	  $http.post(_COMMON.context + url, null, config)
	    .success(function(obj) {
	          notif({
	              msg: "<b><i class='fa fa-thumbs-o-up'></i>  Save Success !</b>",
	              type: "success",
	              position: "center"
	          });
              $location.path('/admin/category/list'); //redirect to admin dashboard.
          })
          .error(function() {
              alert('Error is occured, May be database connect is wrong');
          });
    };

}
